﻿using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveListEditDaoRequestForExportServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveEditDaoRequestRequest, RetrieveEditDaoRequestForExelResponse>
    {
        #region Properties
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord Filter = null;
        private UserRecord currentUser;
        #endregion
        public RetrieveListEditDaoRequestForExportServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveEditDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            Filter = Request.Filter;
        }

        protected override bool OnExecute(RetrieveEditDaoRequestForExelResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0, totalRecord = 0;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                if (Request.Page != null)
                {
                    pageIndex = Request.Page.PageIndex ?? 0;
                    pageSize = Request.Page.PageSize ?? 0;
                }
                var listUser = Providers.EditDao.RetrieveListEditDaoRequest(out totalRecord, currentUser, Filter, pageSize, pageIndex);
                if (listUser != null && listUser.Count > 0)
                {
                    var listDao = listUser.Select(x => x.Dao).ToList();
                    var listRequest = listUser.Select(x => x.Id).ToList();
                    var listDaoEditDetail = Providers.EditDao.RetrieveListRequestDetailByListRequestId(listRequest);
                    var listDaoCode = Providers.SaleCode.RetrieveListSaleCodeByDao(listDao);

                    List<EditDaoRequestExcelRecord> records = listDaoCode.Join(listDaoEditDetail, x => x.Dao, y => y.Dao, (x, y) => new EditDaoRequestExcelRecord
                    {
                        NewCif = y.Cif,
                        NewDaoName = y.DaoName,
                        NewDeptParent = y.DeptParent,
                        NewDivision = y.Division,
                        NewFromDate = DateTime.Now.ToShortDateString(),
                        NewPositionName = y.DeliveryPoint,
                        NewStaffId = y.StaffId,
                        OldCif = x.Cif,
                        OldDaoName = x.DaoName,
                        OldDeptParent = x.DeptParent,
                        OldDivision = x.Division,
                        OldFromDate = x.FromDate,
                        OldPositionName = x.DeliveryPoint,
                        OldStaffId = x.StaffId,
                        RequestId = y.RequestId,
                        Dao = x.Dao
                    }).ToList();
                    response.Records = records;
                    response.Page = new PageRecord
                    {
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        TotalRecords = totalRecord,
                    };
                }
                else
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", currentUser.Dao);
                    isSuccess = false;
                }

            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = e.Message;
                isSuccess = false;
            }
            return isSuccess;
        }
    }
}
